package com.hospital.controller;

import com.google.gson.Gson;
import com.hospital.dao.impl.WardBedPatientDao;

import com.hospital.model.WardBedPatient;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
/**
 * 处理获取病房详细信息的请求，包括病房状态和床位信息
 */
@WebServlet("/wardDetail")
public class WardDetailServlet extends HttpServlet {
    /** 病房床位患者关联数据访问对象，用于查询病房和床位信息 */
    private WardBedPatientDao wardBedPatientDao = new WardBedPatientDao();
    
    /** JSON转换工具对象，用于处理JSON数据的序列化和反序列化 */
    private Gson gson = new Gson();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) 
            throws ServletException, IOException {
        
        // 设置响应类型和编码
        resp.setContentType("application/json;charset=UTF-8");
        
        // 获取病房ID
        String wardIdStr = req.getParameter("wardId");
        
        try {
            Integer wardId = Integer.parseInt(wardIdStr);
            List<WardBedPatient> bedList = wardBedPatientDao.getWardBedsInfo(wardId);
            
            // 将结果转换为JSON并返回
            String jsonResult = gson.toJson(bedList);
            resp.getWriter().write(jsonResult);
            
        } catch (NumberFormatException e) {
            resp.setStatus(400);
            resp.getWriter().write("{\"error\": \"无效的病房ID\"}");
        }
    }
} 